package what.the.fun.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import what.the.fun.domain.Campaign;
import what.the.fun.domain.Survey;
import what.the.fun.domain.User;
import what.the.fun.services.CampaignService;
import what.the.fun.services.SurveyService;
import what.the.fun.services.UserService;


/**
 * Handles requests for the application home page.
 */
@Controller
public class CampaignController {
	
	@Autowired  
	CampaignService campaignService; // must be declared in servlet-context.xml
	@Autowired
	SurveyService surveyService;
	@Autowired
	UserService userService;
	
	ModelAndView loginMaV = new ModelAndView("/user/login");
	
	@RequestMapping("/camp-list")
	 public ModelAndView campList(HttpSession session) {
	
		List<Campaign> campaignList = campaignService.getDataList();
		ModelMap model = new ModelMap();
		model.put("campaignList", campaignList);
		
		HashMap<String, String> hashmapUserName = new HashMap<String, String>();
		for(Campaign c: campaignList) {
			hashmapUserName.put(c.getUserId(), userService.getUser(c.getUserId()).getName());
		}
		
		model.put("hmUserName", hashmapUserName);
		
		return new ModelAndView("campaign/camp-list", "model", model);
	
	 }
	 
	 @RequestMapping("/camp-create")
	 public ModelAndView createCamp(HttpSession session, @ModelAttribute Campaign campaign) {
		 if (session.getAttribute("userId") != null) {	
			 
			 Map<String, String> map = new HashMap<String, String>();
			 return new ModelAndView("campaign/camp-create", "map", map);
		 } else {
			 return loginMaV;
		 }
	 }

	 
	 @RequestMapping("/camp-insert")
	 public String inserCamp(HttpSession session, @ModelAttribute Campaign campaign) {
		 if (session.getAttribute("userId") != null) {
			  if (campaign != null) {
				  campaign.setUserId(session.getAttribute("userId").toString());
				  campaignService.insertData(campaign);
				  return "redirect:/camp-list";
			  }
			  else {
				  return "redirect:/index";
			  }
		 }
		 else {
			 return "redirect:/login";
		 }
	 }
	 
	 @RequestMapping("/camp-delete")
	 public String deleteCamp(HttpSession session, @RequestParam String campId) {
		 if (session.getAttribute("userId") != null) {
			 campaignService.deleteData(campId);
			 return "redirect:/camp-list";
		 }
		 else {
			 return "redirect:/login";
		 }
	 }
	 
	 @RequestMapping("/camp-detail")
	 public ModelAndView detailCamp(HttpSession session, @RequestParam String campId) {
		 Campaign campaign = campaignService.getData(campId);
		 List<Survey> surveyList = new ArrayList<Survey>();
		 surveyList = surveyService.getSurveyListByCampId(campId);
		 ModelMap model = new ModelMap();
		 model.put("surveyList", surveyList);
		 
		 model.addAttribute("campaign", campaign);
		 
		 return new ModelAndView("campaign/camp-detail", "model", model);
	 }
}
